<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar class="nav-bar" title="个人信息">
      <van-icon slot="left" name="arrow-left" @click="$router.back()"/>
    </van-nav-bar>
        <!-- 导航栏 -->
        <input type="file"
        hidden
        ref="file"
        @change="onFileChange"
        >
        <!-- 主体区域 -->
        <van-cell-group>
          <van-cell title="头像" value="内容" is-link>
            <van-image
              class="avater"
                round
                fit="cover"
                :src="user.photo"
                @click="$refs.file.click()"
          />
          </van-cell>
          <van-cell title="昵称"
          :value="user.name"
          @click="isUpdateNameShow = true"
          is-link/>
          <van-cell title="性别"
          :value="user.gender===0 ? '男':'女'"
          is-link
          @click="isUpdateGenderShow = true"
          />
          <van-cell title="生日"
          :value="user.birthday"
          @click="isUpdateAgeShow = true"
          is-link/>

        </van-cell-group>
        <!-- 主体区域 -->
        <!-- 编辑昵称 -->
        <van-popup v-model="isUpdateNameShow"
        style="height:100%"
        position="bottom"
        >
        <!-- 编辑昵称 -->
        <UpdateName  @close="isUpdateNameShow = false"
        v-model="user.name"
        v-if="isUpdateNameShow"
        />
        </van-popup>
        <!-- 编辑昵称结束 -->
        <!-- 编辑性别 -->
        <van-popup v-model="isUpdateGenderShow"
              position="bottom"
            >
            <UpdateGender @close="isUpdateGenderShow = false"
            v-model="user.gender"
            v-if="isUpdateGenderShow"
            />
        </van-popup>
        <!-- 编辑性别  -->
        <!-- 编辑年龄 -->
        <van-popup v-model="isUpdateAgeShow"
              position="bottom"
            >
        <UpdateBirthday
        v-if="isUpdateAgeShow"
        v-model="user.birthday"
        @close="isUpdateAgeShow = false"
        />
        </van-popup>
        <!-- 编辑年龄  -->
        <!-- 编辑头像 -->
        <van-popup v-model="isUpdatePhotoShow"
              style="height:100%"
              position="bottom"
            >
       <UpdatePhoto :img="img"
       @close="(isUpdatePhotoShow = false)"
       @update-photo="(user.photo = $event)"
       v-if="isUpdatePhotoShow"
       />
        </van-popup>
        <!-- 编辑头像  -->
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './components/updatename.vue'
import UpdateGender from './components/updateGender.vue'
import UpdateBirthday from './components/updateBirthday.vue'
import UpdatePhoto from './components/updatePhoto.vue'
export default {
  name: 'UserProfile',
  components: { UpdateName, UpdateGender, UpdateBirthday, UpdatePhoto },
  data () {
    return {
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateAgeShow: false,
      isUpdatePhotoShow: false,
      img: null
    }
  },
  created () { this.loadUserProfile() },
  mounted () {

  },

  methods: {
    async loadUserProfile () {
      try {
        const { data } = await getUserProfile()
        console.log(data)
        this.user = data.data
      } catch (err) {
        this.$toast('加载用户信息失败')
      }
    },
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文章对象获取blob数据
      // 图片的src就是这个data
      this.img = window.URL.createObjectURL(file)
      // 展示弹出层
      this.isUpdatePhotoShow = true
      // file-input选择同一个问年间不会触发onchange事件
      // 解决办法,每次使用完毕把value清空
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
  .user-profile {
    .nav-bar {
      background-color: lightblue;
    }
    .avater {
      width: 30px;
      height: 30px;
    }
    .van-popup {
      background-color: #f5f7f9;
    }
  }
</style>
